package com.icss.ah.delivery.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.icss.ah.base.action.BaseAction;
import com.icss.ah.base.bean.EasyuiDataGridJson;
import com.icss.ah.base.bean.Json;
import com.icss.ah.delivery.bean.DeliveryInfoBean;
import com.icss.ah.login.bean.UserInfoBean;
@Controller
@Scope("prototype")
public class DeliveryAddressInfoAction  extends BaseAction{
	private final Logger errlog = Logger.getLogger("E");
	private final Logger infolog = Logger.getLogger("I");
	// json串
	private Json jsonText;
	private DeliveryInfoBean deliveryInfo = new DeliveryInfoBean();
	/**
	 * 进入收/发货地址维护界面
	 * @return
	 */
	public String toDeliveryInfo(){
		return "deliveryInfo";
	}
	/**
	 * 初始化所属地区选择
	 */
	public void initOtherArea(){
		request = this.getRequest();
		String result = "";
		try{
			String code=request.getParameter("code");
			code = code==null ? "" : code;
			String levell=request.getParameter("level");
			levell = levell==null ? "" : levell;
			int level=1;
			try {
				if(levell==""){
					level=1;
				}else{
					 level = Integer.parseInt(levell)+1;
				}
			} catch (NumberFormatException e) {
			    e.printStackTrace();
			}
			HashMap map = new HashMap();
			map.put("code", code);
			map.put("level", level);
			List<Map> theCountryList = deliveryAddressInfoService.queryTheCountryList(map); //修改时查询当前修改的信息
			result = JSONArray.fromObject(theCountryList).toString();
			infolog.info("DeliveryAddressInfoAction|initOtherArea,查询所属地区成功");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("DeliveryAddressInfoAction|initOtherArea,查询所属地区失败");
		} finally {
			str2Resp(result);
		}
	}
	
	/**
	 * 查询收/发货地址信息
	 */
	public void queryDeliveryList(){
		request = this.getRequest();
		String result = "";
		try {
			String userCode=getUserCodeFromSession();
			UserInfoBean userInfo=getUserInfo(userCode);
			String shopId=userInfo.getShopId();   	//获取店铺id 
			
			dataGridJson = new EasyuiDataGridJson();
			HashMap map = new HashMap();
			map.put("shopId",shopId);
			map.put("startNo",Integer.valueOf(getStartIndexMysql()));
			map.put("pageRows",Integer.valueOf(getEndIndexMysql()));
			List<Map> deliveryList = new ArrayList();
			deliveryList = deliveryAddressInfoService.getDeliveryList(map);	//查询列表
			long deliveryCount = deliveryAddressInfoService.getDeliveryCount(map);//查询总记录数
			dataGridJson.setRows(deliveryList);
			dataGridJson.setTotal(deliveryCount);
			result = JSONObject.fromObject(dataGridJson).toString();
			infolog.info("queryDeliveryList|查询地址列表成功");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("queryDeliveryList|查询地址列表失败：",e);
		} finally {
			str2Resp(result);
		}
	}
	
	/**
	 * 查询要修改的地址
	 */
	public void queryTheAddress() {
		request = this.getRequest();
		String result = "";
		try {
			String deliveryid=request.getParameter("deliveryid"); //添加或修改标示符
			HashMap map = new HashMap();
			map.put("deliveryid", deliveryid);
			HashMap theAddressMap = deliveryAddressInfoService.queryTheAddressMap(map); //修改时查询当前修改的信息
			result = JSONObject.fromObject(theAddressMap).toString();
			infolog.info("queryTheAddress|查询要修改的地址成功");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("queryTheAddress|查询要修改的地址失败：",e);
		} finally {
			str2Resp(result);
		}
	}
	/**
	 * 保存地址(添加或修改)
	 * @return
	 */
	public String saveAddress() {
		request = this.getRequest();
		jsonText = new Json();
		try {
			boolean result=false;
			String addEditFlag=request.getParameter("addEditFlag"); //添加或修改标示符
			String uuid = deliveryInfo.getDeliveryid();
			uuid = uuid == null ? "":uuid;
			String userCode=getUserCodeFromSession();
			UserInfoBean userInfo=getUserInfo(userCode);
			String shopId=userInfo.getShopId();   //获取店铺id 
			deliveryInfo.setShopId(shopId);
			result = deliveryAddressInfoService.saveAddress(deliveryInfo,addEditFlag,uuid);
			if(result==true){
				jsonText.setSuccess(true);
				jsonText.setMsg("操作成功！");
			}else{
				jsonText.setSuccess(false);
				jsonText.setMsg("操作失败！");
			}
		} catch (RuntimeException e) {
			e.printStackTrace();
		}
		return "jsonText";
	}
	
	/**
	 * 删除地址
	 * @return
	 */
	public String delAddress() {
		request = this.getRequest();
		jsonText = new Json();
		try {
			boolean result=false;
			String deliveryid=request.getParameter("deliveryid"); 
			deliveryid = deliveryid == null ? "":deliveryid;
			result = deliveryAddressInfoService.delAddress(deliveryid);
			if(result==true){
				jsonText.setSuccess(true);
				jsonText.setMsg("删除成功！");
			}else{
				jsonText.setSuccess(false);
				jsonText.setMsg("删除失败！");
			}
		} catch (RuntimeException e) {
			e.printStackTrace();
		}
		return "jsonText";
	}
	public Json getJsonText() {
		return jsonText;
	}
	public void setJsonText(Json jsonText) {
		this.jsonText = jsonText;
	}
	public DeliveryInfoBean getDeliveryInfo() {
		return deliveryInfo;
	}
	public void setDeliveryInfo(DeliveryInfoBean deliveryInfo) {
		this.deliveryInfo = deliveryInfo;
	}
}
